package com.bw.coupon.projectInitiation;

import com.bw.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

//@Component
public class ProjectBegin implements ApplicationRunner {

    @Autowired
    private RedisService redisService;

    // 库存预热：加载库存到 Redis
    @Override
    public void run(ApplicationArguments args) throws Exception {
         int productId = 1; // 假设我们的商品ID为1
         int initialStock = 100;
         //防止雪崩
        Long num = (long) (new Random().nextInt(9000) + 1000);
        redisService.setCacheObject("product:stock:" + productId, String.valueOf(initialStock), num, TimeUnit.MINUTES);
    }
}
